<?php

    if (!defined('BASEPATH'))
        exit('No direct script access allowed');

    if (!function_exists('compareItems')) {

        /**
         * Comparison function
         * 
         * @param   $a: stdclass, $b: stdclass
         * 
         * @return  0, -1, 1
         */
        function compareItems($a, $b) {
            if ($a->cnt == $b->cnt)
                return 0;

            return ($a->cnt > $b->cnt) ? -1 : 1;
        }
        
        function compareItems2($a, $b) {
            if ($a->object_no_visits == $b->object_no_visits)
                return 0;

            return ($a->object_no_visits > $b->object_no_visits) ? -1 : 1;
        }

    }

    if (!function_exists('sort_attributes')) {

        function sort_attributes_visits(&$unique) {


            $col = 'object_no_visits';
            $sort = array();
            foreach ($unique as $i => $obj) {
                $sort[$i] = $obj->{$col};
            }

            $sorted_db = array_multisort($sort, SORT_DESC, $unique);

            return $unique;
        }

        function sort_attributes(&$unique) {
            // procedural:
            uasort($unique, 'compareItems');


            // procedural:
            //uasort ( $collection, 'my_sort_function' );
            // Object Oriented
            //uasort ( $collection, array ($this, 'mySortMethod') );
            // Objet Oriented with static method
            //uasort ( $collection, array ('self', 'myStaticSortMethod') );
        }

    }
?>
